import { ref } from "vue";

function useCountdown() {
  const timer = ref();
  const timeEnable = ref(true);
  const timeCount = ref();

  const onStartCountdown = (TIME_COUNT = 60) => {
    if (!timer.value) {
      timeCount.value = TIME_COUNT;
      timeEnable.value = false;
      timer.value = setInterval(() => {
        if (timeCount.value > 0 && timeCount.value <= TIME_COUNT) {
          timeCount.value--;
        } else {
          timeEnable.value = true;
          clearInterval(timer.value);
          timer.value = null;
        }
      }, 1000);
    }
  };

  return {
    onStartCountdown,
    timeEnable,
    timeCount,
  };
}

export default useCountdown;
